package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.vo.TimeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;


    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param dto 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(ReservationDto dto)
    {
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(dto, reservation);
        reservation.setStatus(0);
        return save(reservation) ? 1 : 0;
    }



    @Override
    public int getCancelledReservationCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long userId = UserThreadLocal.getUserId();
        return reservationMapper.getCancelledReservationCount(userId, startTime, endTime);

    }
    /**
     * 查询每个时间段剩余的预约次数
     * @return
     */
    @Override
    public List<TimeVo> countByTime(Long time) {
        //将时间戳转化为时间
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.countByTime(startTime, endTime);
    }

    /**
     *取消预约
     * @param id
     */

    @Override
    public void cancel(Long id) {
      /*  update(Wrappers.<Reservation>lambdaUpdate()
                .set(Reservation::getStatus, 2)
                .eq(Reservation::getId, id)
        );*/
         Reservation reservation =new Reservation();
         reservation.setId(id);
         reservation.setStatus(2);
         updateById(reservation);
    }
    /**
     * 定时更新预约状态
     */
    @Override
    public void updateReservationStatus() {

       /* update(Wrappers.<Reservation>lambdaUpdate()
                .set(Reservation::getStatus, 3)
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now())
        );*/
        List<Reservation> list = list(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now()));

        // 2.修改状态为3
        list.forEach(item -> {
            item.setStatus(3);
        });
        // 3.批量更新
        updateBatchById(list);

    }
}
